package scooper.cn.sc_base;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;

import java.util.UUID;

/**
 * 生成或获取程序安装码
 * （在大多数情况是机器的 设备型号 + SerialNumber ，如果获取不到则为第一次加载时随机生成的 UUID）
 *
 * Created by jiangwj on 2015/1/28.
 */
public final class InstallCode {

    private static final String _PREF = "_InstallCode";

    public static String request(Context context) {
        SharedPreferences pref = context.getSharedPreferences(_PREF, Context.MODE_PRIVATE);
        String uuid = pref.getString("code", null);
        if (uuid == null) {
            uuid = create();
            // store
            SharedPreferences.Editor editor = pref.edit();
            editor.putString("code", uuid);
            editor.commit();
        }
        return uuid;
    }

    private static String create() {
        String uuid;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
            uuid = UUID.randomUUID().toString();
        } else {
            if ("0123456789ABCDEF".equalsIgnoreCase(Build.SERIAL)) {
                uuid = UUID.randomUUID().toString();
            } else {
                uuid = Build.DEVICE + Build.SERIAL;
            }
        }
        return uuid;
    }

}
